<template>
  <div class="app-container">
    <el-button
      type="primary"
      @click="handleExcel"
    >导出</el-button>
    <div class="flex-between">
      <div />
      <div>
        <el-date-picker
          v-model="value1"
          class="search-main"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="timestamp"
          :picker-options="pickerOptions"
        />
        <el-button
          type="primary"
          @click="handleSearch"
        >查询</el-button>
      <!-- <el-input
        v-model="search"
        class="search-main"
        placeholder="请输入用户的名字查询"
        prefix-icon="el-icon-search"
        @keyup.enter.native="HandleSearch"
      /> -->
      </div>
    </div>
    <el-table
      :data="prescriptionList"
      style="width: 100%;margin-top:30px;"
      border
    >
      <el-table-column
        align="center"
        label="编号"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.adminId }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作人员"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.adminName }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="开具药单数"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.prescriptionNum }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="开药总额"
        width=""
      >
        <template slot-scope="scope">
          ￥{{ scope.row.commissionAmount }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="挂号提成"
      >
        <template slot-scope="scope">
          ￥{{ scope.row.registrationFeeAmount }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="总提成"
      >
        <template slot-scope="scope">
          ￥{{ scope.row.amount }}
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :current-page="pageNum"
      :page-size="pageSize"
      layout="total,sizes,  prev, pager, next, jumper"
      :total="totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getStatistic, getExport } from '@/api/admin'
import { timestampToTime } from '@/utils/time'
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      value1: '',
      value2: '',
      routes: [],
      prescriptionList: [],
      dialogVisible: false,
      checkStrictly: false,
      prescription: {},
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      pageNum: 1, // 默认每页显示1条数据
      pageSize: 10,
      loading: false,
      totalCount: 0,
      search: ''
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    // this.getRoutes()

    this.getPrescription({
      pageNum: this.pageNum,
      pageSize: this.pageSize
    })
  },
  methods: {
    async getPrescription(data) {
      const res = await getStatistic(data)
      console.log(res)
      // for (const i in res.data.list) {
      //   res.data.list[i].createTime = timestampToTime(res.data.list[i].createTime)
      //   res.data.list[i].type = res.data.list[i].type === 1 ? '入库' : '出库'
      // }
      this.prescriptionList = res.data.list
      this.totalCount = res.data.total
    },
    generateArr(routes) {
      let data = []
      routes.forEach(route => {
        data.push(route)
        if (route.children) {
          const temp = this.generateArr(route.children)
          if (temp.length > 0) {
            data = [...data, ...temp]
          }
        }
      })
      return data
    },
    handleEdit(scope) {

    },
    handleSizeChange(page) {
      this.pageSize = page
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    handleCurrentChange(page) {
      this.pageNum = page
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })
    },
    cancel() {
      this.dialogVisible = false
    },
    handleSearch() {
      console.log(this.value1)
      this.getPrescription({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        createTimeStart: this.value1[0],
        createTimeEnd: this.value1[1]
      })
    },
    handleInAddPrescription() {
      this.$router.push('/doctor/prescriptionDetail/:prescriptionId')
    },
    async handleExcel() {
    //  const res = await getExport()
    //  console.log(res)
      // this.downLoadExcel(this.prescriptionList, 'aa')
      window.open(process.env.VUE_APP_BASE_API+'/adminCommission/export')
    },
    downLoadExcel(data, fileName) {
      // 定义表头
      let str = `id,用户,销售额,挂号费提成,总提成,挂号提成\n`
      // 增加\t为了不让表格显示科学计数法或者其他格式
      for (let i = 0; i < data.length; i++) {
        for (const item in data[i]) {
          str += `${data[i][item] + '\t'},`
        }
        str += '\n'
      }
      // encodeURIComponent解决中文乱码
      const uri = 'data:text/xls;charset=utf-8,\ufeff' + encodeURIComponent(str)
      // 通过创建a标签实现
      const link = document.createElement('a')
      link.href = uri
      // 对下载的文件命名
      link.download = `${fileName || '表格数据'}.xls`
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
